namespace CIS
{
    using CISLib;
    using System;
    using System.DirectoryServices;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class login : Page
    {
        protected ImageButton iBtnLoginIn;
        protected DropDownList LanguageDdl;
        protected TextBox TextBox1;
        protected TextBox Textbox2;

        private void Authentication(string userName)
        {
            if (ManageUser.IsExistSameUserName(userName))
            {
                UserPrincipal userPrincipal = new UserPrincipal(ManageUser.GetUserViaName(userName), this.Context.User.Identity);
                this.Page.Session.Clear();
                this.Page.Session.Add("language", this.LanguageDdl.SelectedItem.Value);
                this.Context.User = userPrincipal;
                FormsAuthentication.RedirectFromLoginPage(userName, false);
            }
        }

        private void iBtnLoginIn_Click(object sender, ImageClickEventArgs e)
        {
            this.Authentication(this.TextBox1.Text.Trim());
        }

        private void InitializeComponent()
        {
            this.iBtnLoginIn.Click += new ImageClickEventHandler(this.iBtnLoginIn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.LanguageDdl.Items.Clear();
            ListItem cnItem = new ListItem();
            cnItem.Value = "cn";
            cnItem.Text = "中文";
            this.LanguageDdl.Items.Add(cnItem);
            ListItem enItem = new ListItem();
            enItem.Value = "en";
            enItem.Text = "English";
            this.LanguageDdl.Items.Add(enItem);
        }

        private bool LDAPAudit(string usrName, string usrPWD)
        {
            if (usrPWD == "")
            {
                base.Response.Write("<script> alert('密码不能为空！') </script>");
                return false;
            }
            DirectoryEntry root = null;
            DirectoryEntry entry = null;
            try
            {
                string ldapUrl = "LDAP://oa2.alcatel-sbell.com.cn/ou=cn,o=alcatel";
                string filter = "(cn=" + usrName + ")";
                root = new DirectoryEntry(ldapUrl);
                root.AuthenticationType = AuthenticationTypes.ServerBind;
                DirectorySearcher searcher = new DirectorySearcher(root);
                searcher.Filter = filter;
                searcher.PropertyNamesOnly = true;
                searcher.PropertiesToLoad.Add("cn");
                SearchResult result = searcher.FindOne();
                root.Close();
                root = null;
                string sDn = "";
                sDn = result.Path;
                int pos = sDn.LastIndexOf("/");
                sDn = sDn.Substring(pos + 1);
                entry = new DirectoryEntry(ldapUrl, sDn, usrPWD, AuthenticationTypes.None);
                searcher = new DirectorySearcher(entry);
                searcher.Filter = filter;
                searcher.PropertyNamesOnly = true;
                searcher.PropertiesToLoad.Add("cn");
                result = searcher.FindOne();
                entry.Close();
                entry = null;
                return true;
            }
            catch
            {
                try
                {
                    if (root != null)
                    {
                        root.Close();
                    }
                }
                catch
                {
                }
                try
                {
                    if (entry != null)
                    {
                        entry.Close();
                    }
                }
                catch
                {
                }
            }
            base.Response.Write("<script> alert('密码错！') </script>");
            return false;
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.InitPage();
            }
        }
    }
}

